package com.lwm.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwm.domain.FactoryDetail;
import com.lwm.domain.FactoryModeling;
import com.lwm.domain.ResponseResult;
import com.lwm.dto.FactoryIdDto;
import com.lwm.dto.FactoryModelingDto;
import com.lwm.mapper.FactoryDetailMapper;
import com.lwm.mapper.FactoryModelingMapper;
import com.lwm.service.FactoryModelingService;
import com.lwm.utils.Code;
import com.lwm.utils.HintMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 工厂建模业务层
 */
@Service
public class FactoryModelingServiceImpl implements FactoryModelingService {
    @Resource
    private FactoryModelingMapper factoryModelingMapper;
    @Resource
    private FactoryDetailMapper factoryDetailMapper;

    /**
     * 保存
     *
     * @param factoryModelingDto 工厂建模dto
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> save(FactoryModelingDto factoryModelingDto) {
        // 必填项 生产线名称、产品(物料档案)
        if (!StringUtils.hasText(factoryModelingDto.getProductLineName())
                || null == factoryModelingDto.getRecordId()
        ) {
            return new ResponseResult<>(Code.SAVE_ERR, HintMessage.MUST_PARAM_HINT);
        }
        // 执行插入 工厂建模数据
        Long factoryId = IdUtil.getSnowflakeNextId();// 工厂建模新数据的id
        factoryModelingDto.setId(factoryId);
        factoryModelingMapper.insert(factoryModelingDto);

        // 执行修改 工厂建模明细数据-工厂建模id、物料档案id
        List<Long> detailIds = factoryModelingDto.getDetailIds();
        FactoryDetail factoryDetail = new FactoryDetail();
        factoryDetail.setFactoryId(factoryId);
        factoryDetail.setRecordId(factoryModelingDto.getRecordId());
        // 遍历出明细id
        detailIds.forEach(detailId -> {
            // 设置上去
            factoryDetail.setId(detailId);
            // 调用修改方法
            factoryDetailMapper.update(factoryDetail);
        });
        return new ResponseResult<>(Code.GET_OK, HintMessage.SAVE_OK_HINT);
    }

    /**
     * 删除
     *
     * @param factoryIdDto 工厂建模id和明细id dto
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> remove(FactoryIdDto factoryIdDto) {
        // factoryIds为null 或 没有传入工厂建模id
        if (null == factoryIdDto.getFactoryIds() || factoryIdDto.getFactoryIds().isEmpty()) {
            return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_LACK_ID + "工厂建模");
        }
        int i = factoryModelingMapper.deleteByIds(factoryIdDto.getFactoryIds());
        // 明细id存在的情况下，再去删除明细数据
        if (null != factoryIdDto.getDetailIds() && !factoryIdDto.getDetailIds().isEmpty()) {
            factoryDetailMapper.deleteByIds(factoryIdDto.getDetailIds());
        }
        if (i <= 0) return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_ERR_HINT);
        return new ResponseResult<>(Code.DELETE_OK, HintMessage.REMOVE_OK_HINT);
    }

    /**
     * 编辑
     *
     * @param factoryModelingDto 工厂建模dto
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> edit(FactoryModelingDto factoryModelingDto) {
        // 缺乏id，给予提示
        if (null == factoryModelingDto.getId()) {
            return new ResponseResult<>(Code.UPDATE_ERR,
                    HintMessage.EDIT_LACK_ID + "工厂建模");
        }
        // 执行 工厂建模 修改
        factoryModelingMapper.update(factoryModelingDto);
        // 执行 工厂建模明细修改
        List<Long> detailIds = factoryModelingDto.getDetailIds();
        FactoryDetail factoryDetail = new FactoryDetail();
        factoryDetail.setFactoryId(factoryModelingDto.getId());
        factoryDetail.setRecordId(factoryModelingDto.getRecordId());
        // 遍历出明细id
        detailIds.forEach(detailId -> {
            // 设置上去
            factoryDetail.setId(detailId);
            // 调用修改方法
            factoryDetailMapper.update(factoryDetail);
        });
        return new ResponseResult<>(Code.UPDATE_OK, HintMessage.EDIT_OK_HINT);
    }

    /**
     * 分页
     *
     * @param page            页码
     * @param size            数量
     * @param factoryModeling 工厂建模
     * @return {@link ResponseResult}<{@link PageInfo}<{@link FactoryModelingDto}>>
     */
    @Override
    public ResponseResult<PageInfo<FactoryModelingDto>> page(Integer page, Integer size, FactoryModeling factoryModeling) {
        // 设置默认值
        if (null == page) page = 1;
        if (null == size) size = 5;
        PageInfo<FactoryModelingDto> pageInfo = PageHelper.startPage(page, size)
                .doSelectPageInfo(() -> factoryModelingMapper.selectAll(factoryModeling));
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, pageInfo);
    }

    /**
     * 根据id获取
     *
     * @param id id
     * @return {@link ResponseResult}<{@link FactoryModelingDto}>
     */
    @Override
    public ResponseResult<FactoryModelingDto> getById(Long id) {
        // 没有传递id值
        if (null == id) return new ResponseResult<>(Code.GET_ERR, HintMessage.GET_LACK_ID + "数据");
        FactoryModelingDto factoryModeling = factoryModelingMapper.selectById(id);
        // 没有查询到数据
        if (Objects.isNull(factoryModeling)) {
            return new ResponseResult<>(Code.GET_ERR, HintMessage.GET_ERR_HINT);
        }
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, factoryModeling);
    }
}
